package com.example.java_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-08-05
 * Time: 14:31
 */

/**
 * 用来管理用户的在线状态
 */
@Component
public class OnlineUserManager {
    // gameHall中存在键值对说明此用户在游戏大厅中在线
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    // gameRoom中存在键值对说明此用户在游戏房间中在线
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    // 进入游戏大厅
    public void enterGameHall(int userId, WebSocketSession webSocketSession) {
        gameHall.put(userId, webSocketSession);
    }
    // 退出游戏大厅
    public void exitGameHall(int userId) {
        gameHall.remove(userId);
    }
    // 从游戏大厅中取出session，用来判断用户是否在游戏大厅
    public WebSocketSession getFromGameHall(int userId) {
        return gameHall.get(userId);
    }
    // 进入游戏房间
    public void enterGameRoom(int userId, WebSocketSession session) {
        gameRoom.put(userId, session);
    }
    // 退出游戏房间
    public void exitGameRoom(int userId) {
        gameRoom.remove(userId);
    }
    // 从游戏大厅中取出session，用来判断用户是否在游戏房间
    public WebSocketSession getSessionFromGameRoom(int userId) {
        return gameRoom.get(userId);
    }
}
